function getBlogResult(obj,offset,q){
	//$(obj).unbind('click');
	$(obj).removeAttr('onclick').click(function(){});
	
	$.ajax({
		beforeSend: function(){
			if($("#get_more").length > 0){
				$("#get_more").html('Loading...');
			}
			else{
				$("#relative_articles_content").html('<div id="get_blog_result_loading"><img src="/images/public/ajax-loader.gif" /></div>');	
			}
	    },
	    //dataType: 'json',
		type : "POST",
		async : true,
		url : "/activity/get_blog_result",
		cache : true,
		data : {
			start:offset,
			q:q
		},
		success : function(data) {
			if($('#get_blog_result_loading').length>0){
				$('#get_blog_result_loading').remove();
			}
			
			if($("#get_more").length > 0){
				$("#get_more").remove();
			}
			
			$("#relative_articles_content").append(data);
		}
	});	
	
	return false;
}
function saveAsDraft(){
	//$('#save_activity').get(0).setAttribute('action', "/activity_edit/save_as_draft"); //this works
	 
	$("#save_activity").attr("action", "http://www.coplay.cc/activity_edit/save_as_draft");
	$('#save_activity').submit();
	return false;
}

function unbindOnBeforeUnload(){
	window.document.body.onbeforeunload=null;return true;
}

function deleteTempFiles(){
	var values = new Array();
	$.each($("input[name='upload_image[]']"), function() {
  		values.push($(this).val());
	});
	var imagesJsonString = JSON.stringify(values);
	
	$.ajax({
			beforeSend: function(){
	        },
            type: "POST",
            async : false,
            url: "/image_uploader/delete_temp_files",
            cache: true,
            data: {
                upload_image: imagesJsonString
        	},
            success: function(data){
            }
        });
        
    return false;
} 

function askConfirm()
{
	return "★★您的文件尚未儲存，確定要離開網頁？★★";
}

function deleteActivity(activity_id,kind){
	
	x = confirm("確定要刪除？")
	if (x) {
		$.ajax({
			beforeSend: function(){
	            $("#delete_button").html('<img src="/images/public/ajax-loader.gif" />');
	        },
            type: "POST",
            url: "/activity_edit/delete",
            cache: false,
            data: {
                activity_id: activity_id,
                kind: kind
        	},
            success: function(data){
            	$('#'+kind+'_tab_content').html(data);
            }
        });		
	}	
}

function cancelJoin(activity_id,kind){
	
	x = confirm("確定要取消參加？")
	if (x) {
		$.ajax({
			beforeSend: function(){
	            $("#cancel_join_button").html('<img src="/images/public/ajax-loader.gif" />');
	        },
            type: "POST",
            url: "/activity_edit/cancel_join",
            cache: false,
            data: {
                activity_id: activity_id,
                kind: kind
        	},
            success: function(data){
            	$('#'+kind+'_tab_content').html(data);
            }
        });		
	}	
}

function loginForm(){
	$.facebox(function(){
		$.ajax({
            type: "POST",
            url: "/auth/login",
            cache: false,
            dataType: 'json',
            data: {
                
        	},
            success: function(data){
            	$.facebox(data['view']);
            	/*
            	$("#temp_response_content").html(data);
            	 $("#temp_response_content").find("script").each(function(i) {
                    eval($(this).text());
                });
            	$.facebox($("#temp_response_content").html());
            	$('#temp_response_content').empty();
            	*/
            }
        });		
	});
}

function logoutForm(){
	$.facebox(function(){
		$.ajax({
            type: "POST",
            url: "/auth/logout",
            cache: false,
            data: {
                
        	},
            success: function(data){
            	$.facebox(data);
            	location.reload();
            }
        });		
	});
}

function registerForm(){
	$.facebox(function(){
		$.ajax({
            type: "POST",
            url: "/auth/register",
            cache: false,
            data: {
                
        	},
            success: function(data){
            	$.facebox(data);
            }
        });		
	});
}

function sendMessageForm(sendto){
	$.facebox(function(){
		$.ajax({
            type: "POST",
            url: "/personal/send_message",
            cache: false,
            dataType: 'json',
            data: {
            	sendto: sendto
        	},
            success: function(data){
            	$.facebox(data['view']);
	       }
        });		
	});
}

function forgotPasswordForm(){
	$.facebox(function(){
		$.ajax({
            type: "POST",
            url: "/auth/forgot_password",
            cache: false,
            data: {
                
        	},
            success: function(data){
            	$.facebox(data);
            }
        });		
	});
}

function register(){
	$.facebox(function(){
		$.ajax({
            type: "POST",
            url: "/auth/register",
            cache: false,
            data: {
                email: $('#email').val(),
                password: $('#password').val(),
                confirm_password: $('#confirm_password').val(),
        	},
            success: function(data){
            	$.facebox(data);
            }
        });		
	});
}

function OnLoading() {
	//alert("I am loading");
	$.facebox('<div style="width:100%;text-align:center;"><img src="/images/public/ajax-loader.gif" /></div>');
}

function OnSuccess(data) {
	//alert("request completed ");
	$.facebox(data);
}

function OnError(XML) {
	alert(XML);
}

function SendLoginData() {
	var AjaxForm = new AjaxFormSubmitter("login_form");
	AjaxForm.SetURL("/auth/login");
	AjaxForm.SetMethod("POST");
	AjaxForm.SetDataType("json");

	AjaxForm.SetOnSuccess(function(data) {
		
		$.facebox(data['view']);
		if(data['reload_page']=='1'){
			location.reload();
		}
	});

	AjaxForm.SetOnFailure(function(error) {
		//OnError(error);
	});

	AjaxForm.SetOnLoading(function() {
		OnLoading();
	});

	AjaxForm.Submit();
}

function SendRegisterData() {
	var AjaxForm = new AjaxFormSubmitter("register_form");
	AjaxForm.SetURL("/auth/register");
	AjaxForm.SetMethod("POST");

	AjaxForm.SetOnSuccess(function(data) {
		OnSuccess(data);
	});

	AjaxForm.SetOnFailure(function(error) {
		OnError(error);
	});

	AjaxForm.SetOnLoading(function() {
		OnLoading();
	});

	AjaxForm.Submit();
}

function SendActivateMailAgainData() {
	var AjaxForm = new AjaxFormSubmitter("send_again_form");
	AjaxForm.SetURL("/auth/send_again");
	AjaxForm.SetMethod("POST");
	AjaxForm.SetDataType("json");

	AjaxForm.SetOnSuccess(function(data) {
		
		$.facebox(data['view']);
		if(data['reload_page']=='1'){
			location.reload();
		}
	});

	AjaxForm.SetOnFailure(function(error) {
		//OnError(error);
	});

	AjaxForm.SetOnLoading(function() {
		OnLoading();
	});

	AjaxForm.Submit();
}


function SendForgotPasswordData() {
	var AjaxForm = new AjaxFormSubmitter("forgotpassword_form");
	AjaxForm.SetURL("/auth/forgot_password");
	AjaxForm.SetMethod("POST");

	AjaxForm.SetOnSuccess(function(data) {
		OnSuccess(data);
	});

	AjaxForm.SetOnFailure(function(error) {
		OnError(error);
	});

	AjaxForm.SetOnLoading(function() {
		OnLoading();
	});

	AjaxForm.Submit();
}

function SendUpdateProfileData() {
	var AjaxForm = new AjaxFormSubmitter("updateprofile_form");
	AjaxForm.SetURL("/personal/update_profile");
	AjaxForm.SetMethod("POST");
	AjaxForm.SetDataType("json");

	AjaxForm.SetOnSuccess(function(data) {
		$("#user_profile_tab_content").html(data['view']);
		if(data['update_status']=='1'){
			$("#update_loading").html('<img src="/images/public/Ok-icon.png" />');	
		}
	});

	AjaxForm.SetOnLoading(function() {
		$("#update_loading").html('<img src="/images/public/ajax-loader-circle.gif" />');
	});

	AjaxForm.Submit();
}

function SendChangePasswordData() {
	var AjaxForm = new AjaxFormSubmitter("changepassword_form");
	AjaxForm.SetURL("/personal/change_password");
	AjaxForm.SetMethod("POST");
	AjaxForm.SetDataType("json");

	AjaxForm.SetOnSuccess(function(data) {
		$("#change_password_tab_content").html(data['view']);
		if(data['update_status']=='1'){
			$("#cp_loading").html('<img src="/images/public/Ok-icon.png" />');	
		}
	});

	AjaxForm.SetOnLoading(function() {
		$("#cp_loading").html('<img src="/images/public/ajax-loader-circle.gif" />');
	});

	AjaxForm.Submit();
}

function SendNewMessageData() {
	var AjaxForm = new AjaxFormSubmitter("sendmessage_form");
	AjaxForm.SetURL("/personal/send_message");
	AjaxForm.SetMethod("POST");
	AjaxForm.SetDataType("json");

	AjaxForm.SetOnSuccess(function(data) {
		if(data['send_status']=='0'){
			if(data['reload_page']=='1'){
				location.reload();
			}
			else{
				$.facebox(data['view']); // show failed msg
			}
		}
		else{
			$.facebox(data['view']); //show success msg
		}
	});

	AjaxForm.SetOnLoading(function() {
		OnLoading();
	});

	AjaxForm.Submit();
}

function replyMessage(){
	var recipient_id = $('#recipient_id_for_reply_msg').val();
	$.ajax({
			beforeSend: function(){
	            $("#messages_by_sender").html('<img src="/images/public/ajax-loader.gif" />');
	        },
            type: "POST",
            url: "/personal/reply_message",
            cache: false,
            dataType: 'json',
            data: {
                recipient_id: recipient_id,
                body: $('#reply_msg').val()
        	},
            success: function(data){
            	$("#messages_by_sender").html(data);
            	
            	if(data['status']=='0'){
					if(data['reload_page']=='1'){
						location.reload();
					}
					else{
						alert(data['msg']);
					}
				}
				else{
					getMessagesBySender(recipient_id);
				}
            }
        });	
}

function getMessagesBySender(sender_id){
	$.ajax({
			beforeSend: function(){
	            $("#messages_by_sender").html('<img src="/images/public/ajax-loader.gif" />');
	        },
            type: "POST",
            url: "/personal/get_messages_by_sender",
            cache: false,
            dataType: 'json',
            data: {
                sender_id: sender_id
        	},
            success: function(data){
            	$("#messages_by_sender").html(data);
            	
            	if(data['status']=='0'){
					if(data['reload_page']=='1'){
						location.reload();
					}
					else{
						//$.facebox(data['view']); // show failed msg
						$("#messages_by_sender").html(data['view']);
					}
				}
				else{
					//$.facebox(data['view']); //show success msg
					$("#messages_by_sender").html(data['view']);
					var textarea = document.getElementById('messages_by_sender_content');
					textarea.scrollTop = textarea.scrollHeight;
				}
            }
        });		
}
